<?php
session_start();
require('../FPDF/fpdf.php');
include("../includes/conexion.php");
class PDF extends FPDF
{
//Page header
function Header()
{
    $fechainicio=date('01/11/2008');
    $this->SetFont('Arial','B',14);
	//Move to the right
	$this->Cell(1);
	//Title
	$this->Cell(188,15,'UNIVERSIDAD NACIONAL  AGRARIA DE LA SELVA',0,0,'C');
	//Line break
	$this->Ln(14);

	$this->SetFont('Arial','B',13);
	//Move to the right
	$this->Cell(1);
	//Title
	$this->Cell(188,13,'Reporte Ingreso Documentos por usuario',0,0,'C');
	//Line break
	$this->Ln(13);
	$this->SetFont('Arial','',9);
	//Move to the right
	$this->Cell(1);
	//Title
	$fechas=date("d/m/Y");
	//$pages=split('/',$this->PageNo().'/{nb}');
	//$this->Cell(15,10,'DEL',0,0,'L')+$this->Cell(20,10,$fechainicio,0,0,'R')+$this->Cell(20,10,'AL',0,0,'C')+$this->Cell(20,10,$fechas,0,0,'R')+$this->Cell(65,10,'PAGINA',0,0,'R')+$this->Cell(15,10,$pages[0],0,0,'C')+$this->Cell(15,10,'DE',0,0,'C')+$this->Cell(15,10,$pages[1],0,0,'C');
	//Line break
	$this->Ln(10);
	
    
}

//Page footer
function Footer()
{
	//Position at 1.5 cm from bottom
	$this->SetY(-15);
	//Arial italic 8
	$this->SetFont('Arial','I',8);
	//Page number
	$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}


function LoadData()
{

$rrhh = $_GET['id_rrhh'];
$filtro = $_GET['filtro'];
$por_dia = $_GET['por_dia'];
$fecha_ini = $_GET['fecha_ini'];
$fecha_fin = $_GET['fecha_fin'];
$por_mes = $_GET['por_mes'];
$por_ano = $_GET['por_ano'];

if($rrhh) $wh_rr = " and d.rrhh_ingreso_documento=$rrhh ";

switch($filtro){
		 case 'por_dia':
		      $wh = " and left(rrhh_fecha_ingreso,10)='".retorna_fecha($por_dia)."' "; 	
			  break;
		 case 'por_semana':
		      $wh = " and left(rrhh_fecha_ingreso,10) between '".retorna_fecha($fecha_ini)."' and '".retorna_fecha($fecha_fin)."' "; 			
			  break;
		 case 'por_mes':
		      $wh = " and year(rrhh_fecha_ingreso)='".date('Y')."' and month(rrhh_fecha_ingreso)='$por_mes' "; 					
		      break;
		 case 'por_ano':
		      $wh = " and year(rrhh_fecha_ingreso)='$por_ano' "; 					
		      break;	    
}



global $handle;
$sql_user="
SELECT
CONCAT(nombres,' ',apellidos)as rrhh,
(select count(*) from documento where id_tipo_documento=1 and rrhh_ingreso_documento=d.rrhh_ingreso_documento $wh) as 'libros',
(select count(*) from documento where id_tipo_documento=2 and rrhh_ingreso_documento=d.rrhh_ingreso_documento $wh) as 'tesis',
(select count(*) from documento where id_tipo_documento=3 and rrhh_ingreso_documento=d.rrhh_ingreso_documento $wh) as 'revistas',
(select count(*) from documento where id_tipo_documento=4 and rrhh_ingreso_documento=d.rrhh_ingreso_documento $wh) as 'tifac'
FROM documento d left join rrhh r on d.rrhh_ingreso_documento = r.id_rrhh
where
d.rrhh_ingreso_documento <>0
$wh_rr
group by d.rrhh_ingreso_documento
 

";

//die($sql_user);
     $qry_user=mysql_query($sql_user);
	$data=array();
	while($lines=mysql_fetch_array($qry_user))
	   	$data[]=$lines;
	return $data;

 
}


function ImprovedTable($header,$data)
{   
  	$rowadd=0;
	//Column widths
	$w=array(100,10,10,10,10);
	$this->Cell(1);
	//Header
	for($i=0;$i<count($header);$i++)
		$this->Cell($w[$i]+8,7,$header[$i],1,0,'C');
	$this->Ln();
	//Data
	$this->Cell(1);
	foreach($data as $row)
	{
	    if ($rowadd==37){
		$this->Ln();
		$this->Cell(1);
		for($i=0;$i<count($header);$i++)
		$this->Cell($w[$i]+8,7,$header[$i],1,0,'C');
		$this->Ln();
		$this->Cell(1);
		$rowadd=0;
		
		}
        $this->Cell($w[0]+8,4,$row[0],1,0,'L');
		$this->Cell($w[1]+8,4,$row[1],1,0,'R');
		$this->Cell($w[2]+8,4,$row[2],1,0,'R');
		$this->Cell($w[2]+8,4,$row[3],1,0,'R');
		$this->Cell($w[2]+8,4,$row[4],1,0,'R');
		$this->Ln();
		$this->Cell(1);
		$rowadd = $rowadd +1;
		$sum = $sum + $row[2];
		
		}	   		
}

}


$pdf=new PDF();
$pdf->AliasNbPages();
//Column titles
$header=array('RRHH','Libro','Tesis','Revistas','Tifac');
//Data loading
$data=$pdf->LoadData();
$pdf->SetFont('Arial','',7);
$pdf->AddPage();
$pdf->ImprovedTable($header,$data);
$pdf->Output();

function retorna_fecha($fecha){
    if($fecha){
       $fecha = explode(" ",$fecha);
	   $fecha = $fecha[0];
       $fecha=explode("/",$fecha);
       $fecha=$fecha[2]."-".$fecha[1]."-".$fecha[0];
       return $fecha;
    }
}


?>
